package com.ibm.spatiotemp.server.mocking;

import java.text.ParseException;

import org.apache.log4j.Logger;

import com.ibm.spatiotemp.server.framework.request.Request;
import com.ibm.spatiotemp.server.framework.request.RequestType;

public class TestRequestFactory {

	public static Logger logger = Logger.getLogger(TestRequestFactory.class);
	
	public static Request generateTenantRequest(String tentid){
		
		Request req = new Request();
		req.setQtype(RequestType.Tenant);
		req.setTenantid(tentid);
		return req;
	}
	
	public static Request generateSpatialQueryRequest(
			String tentid, String srcid, String querytype, String modid, boolean withmovid) throws ParseException{
		Request req = new Request();
		req.setQtype(querytype);
		req.setTenantid(tentid);
		if(querytype.equalsIgnoreCase(RequestType.Firsttime)){
			/*GET /tenant1?queryType=firsttime&
			 * id=uuid1&
			 * position=[25,36]
			 * &distance=5
			 * &start=2014-01-01T00:00:00
			 * &end=2014-02-01T00:00:00*/
			String spatial_req_body = 
					"{\"sourceid\":\"%s\","
					+ "\"id\":\"%s\","
					+ "\"position\":[25,36],"
					+ "\"distance\":5,"
					+ "\"start\":\"2014-01-01T00:00:00\","
					+ "\"end\":\"2014-02-01T00:00:00\""
					+ "}";
			spatial_req_body = String.format(spatial_req_body, srcid, modid);
			req.setRequestContent((spatial_req_body));
		}
		else if(querytype.equalsIgnoreCase(RequestType.Position)){
			/*Method: GET /tenant1?queryType=position
			 * &time=2014-01-01T00:00:20&&
			 * bbox=[-122.40327,37.52015,-122.3101,37.90572]
			 * */
			String spatial_req_body = 
					"{\"sourceid\":\"%s\","
					+ "\"time\":\"2014-01-01T00:00:20.000\","
					+ "\"bbox\":[20,31,30,41]"
					+ "}";
			spatial_req_body = String.format(spatial_req_body, srcid);
			req.setRequestContent((spatial_req_body));
			
		}
		else if(querytype.equalsIgnoreCase(RequestType.Trajectory)){
			if(withmovid)
			{
			/*Method: GET /tenant1
			 * ?queryType=trajectory
			 * &id=uuid1
			 * &start=2014-01-01T00:00:00&
			 * end=2014-02-01T00:00:00*/
			String spatial_req_body = 
					"{\"sourceid\":\"%s\","
					+ "\"id\":\"%s\","
					+ "\"start\":\"2014-01-01T00:00:00\","
					+ "\"end\":\"2014-02-01T00:00:00\"}";
			spatial_req_body = String.format(spatial_req_body, srcid, modid);
			req.setRequestContent((spatial_req_body));
			}
			else
			{
				/*GET /tenant1?
				 * queryType=trajectory
				 * &start=2014-01-01T00:00:00
				 * &end=2014-02-01T00:00:00&
				 * bbox=[-122.40327,37.52015,-122.3101,37.90572]&bboxRel=intersect*/
				String spatial_req_body = 
						"{\"sourceid\":\"%s\","
						+ "\"start\":\"2014-01-01T00:00:00\","
						+ "\"end\":\"2014-02-01T00:00:00\","
						+ "\"bbox\":[20,31,30,41],"
						+ "\"bboxRel\":\"intersect\"}";
				spatial_req_body = String.format(spatial_req_body, srcid);
				req.setRequestContent((spatial_req_body));
			}
		}
		else{
			String bodystring = "{}";
			req.setRequestContent((bodystring));
		}
		return req;
	}
	
	public static Request genearteRawDataQequest(String tentid, String querytype, String modid, String sourcename) throws ParseException{
		Request req = new Request();
		req.setQtype(querytype);
		req.setTenantid(tentid);
		if(querytype.equalsIgnoreCase(RequestType.ACTION_READDATA)){
			/*Method: GET /tenant1
			 * ?queryType=trajectory
			 * &id=uuid1
			 * &start=2014-01-01T00:00:00&
			 * end=2014-02-01T00:00:00*/
			String spatial_req_body = 
					"{\"sourceid\":\"%s\","
					+ "\"id\":\"%s\","
					+ "\"start\":\"2014-01-01T00:00:00\","
					+ "\"end\":\"2014-02-01T00:00:00\"}";
			req.setRequestContent((String.format(spatial_req_body,sourcename,modid)));
		}
		else if(querytype.equalsIgnoreCase(RequestType.ACTION_WRITEDATA)){
		/*
		 * Method: POST /tenant1?id=uuid1
		POST Data:
		[{"t":"2014-01-01T00:00:00.000", "p":{"type": "Point","coordinates": [-50,53] }, "pressure":3.00},
		{"t":"2014-01-01T00:00:20.000", "p":{"type": "Point","coordinates": [25,36] },"pressure":4.00},
		{"t":"2014-01-01T00:00:30.000", "p":{"type": "Point","coordinates": [75,50] }, "pressure":3.00},
		{"t":"2014-01-01T00:00:55.000", "p":{"type": "Point","coordinates": [150,32] }, "pressure":4.00}]
		 */
			String spatial_req_body = 
					"{\"sourceid\":\"%s\","
					+ "\"id\":\"%s\","
					+ "\"data\":%s}";
			String datacontent = 
				"[{\"t\":\"2014-01-01T00:00:00.000\", \"p\":{\"type\": \"Point\",\"coordinates\": [-50,53] }, \"pressure\":3.00},"
				+ "{\"t\":\"2014-01-01T00:00:20.000\", \"p\":{\"type\": \"Point\",\"coordinates\": [25,36] },\"pressure\":4.00},"
				+ "{\"t\":\"2014-01-01T00:00:30.000\", \"p\":{\"type\": \"Point\",\"coordinates\": [75,50] }, \"pressure\":3.00},"
				+ "{\"t\":\"2014-01-01T00:00:55.000\", \"p\":{\"type\": \"Point\",\"coordinates\": [150,32] }, \"pressure\":4.00}]";
			req.setRequestContent((String.format(spatial_req_body,sourcename, modid, datacontent)));
		}
		else{
			String bodystring = "{}";
			req.setRequestContent(bodystring);
		}
		return req;
	}
	
}
